<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/10/15
 * Time: 9:48
 */
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends Controller{
    public function index(){
        if(session('name')){
            $this->assign('username',session('name'));
        }else{
            redirect(U('/Admin/Login/index/'),0,"你已退出！");
        }
        $id = I('id');
        $classification=M("classification");
        $this->classification=$classification->field("id,name")->select();
//        foreach ($this->classification as $k=>$v){
//            var_dump($k);
//            var_dump($v);
//
//        }
//        var_dump($this->classification);
//       exit();
        $this->assign("data",$this->classification);

        if($id){
            $article=M('article');
            $this->article_item=$article->where('`id`='.$id)->find();
//            var_dump($id);
//            exit();
            if($this->article_item){
                $this->assign('article_item',$this->article_item);
                $this->assign('title_text','修改文章');
                $this->assign('btn_ok_text','修改文章');
                $this->assign('btn_ok_act','update');

            }else{
                $this->error("你要修改的文章不存在！");
            }


        }else{
            $this->assign('title_text','新文章');
            $this->assign('btn_ok_text','添加文章');
            $this->assign('btn_ok_act','add');

        }
        $this->assign('username',session('name'));
        $this->assign('title','新闻发布');
        $this->display();
    }
    public function lm(){
        $User = M('classification');
        $list = $User->limit(10)->select();
        $this->assign('list',$list);
    }
    public function add()
    {
        $cid=I("select");

            $article = D('article');
            if($article->create()){
                $article->message=html_entity_decode(I('editorValue'));
                $article->createtime=time();
                $article->author=session('name');
                $article->cid=$cid;
                if($article->add()){

                    $this->success("文章添加成功！",U('/Admin/Index/index/'));
                }else{
//                    echo $article->getlastSql();
//                  exit();
                    $this->error("文章添加失败！",U('/Admin/Index/index/'));
                }
            }else{
                $this->error($article->getError());
            }
//                    var_dump($article);
//            exit();
    }
    public function edit($id){
        if($id){
            redirect(U('/Admin/Article/index/',array('id'=>$id)));
        }else{
            $this->error("非法数据，无法完成修改!");
        }
    }
    public function quit(){
        if(session('name')){
            session(null);
        }
        redirect(U('/Admin/Login/index/'),0,"重新登录");
    }

    public function update(){
        $article = M('article');
        $data = array(
            'wzname' =>I('wzname'),
            'message' =>html_entity_decode(I('editorValue')),
            'lasttime' =>time()
        );
        $id = I('id');
        $flag = $article->where('`id`='.$id)->setField($data);
        if($flag){
            $this->success("修改成功",U('/Admin/Index/index/'));
        }else{
            $this->error("修改失败");
        }
    }

}